package project.euler.honk.problem.to50;


public final class Problem44 {

	private static final int SIZE = 5000;
	private static final boolean[] numbers = new boolean[SIZE * SIZE * 10];

	private static boolean isPentagonal(int number) {
		return numbers[number];
	}

	public static int solve() {
		for (int i = 0; i < numbers.length; i++) {
			int val = i * (3 * i - 1) / 2;
			if(val >= numbers.length) break;
			numbers[val] = true;
		}
		int min = Integer.MAX_VALUE;
		for (int i = 1; i < SIZE; i++) {
			int inum = i * (3 * i - 1) / 2;
			for (int j = i + 1; j < SIZE; j++) {
				int jnum = j * (3 * j - 1) / 2;
				int delta = jnum - inum;
				if (delta > min)
					break;
				if (isPentagonal(jnum - inum)) {
					if (isPentagonal(jnum + inum)) {
						System.out.println(inum + " " + jnum + " " + delta);
						min = delta;
					}
				}
			}
		}
		return min;
	}

	public static void main(String[] args) {
		long start = System.currentTimeMillis();
		long result = solve();
		long end = System.currentTimeMillis();
		System.out.println("Result: " + result + " Time: " + (end - start));
	}

}
